var app=getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {  
    list: [[], [], [], [], [], []],
    pages: [0, 0, 0, 0, 0, 0],
    nexts: [true, true, true, true, true, true],
    size: 10,
    winheight: 0,
    winwidth: 0,
    bars: ["全部(0)元", "待结算(0)元","已结算(0)元",],
    tab:0,  
    topUrl:"/api/delivery/getDeliveryIncomes.do",
    action:"/api/delivery/getDeliverryProfitLists.do",
	submit:"{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var self=this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth;
        self.setData({
          winheight: clientHeight,
          winwidth: clientWidth
        });
      }
    });
    var params={}
      ux.request(this.data.topUrl, options, function (data) {
      console.log(JSON.stringify(data))
        var bars = ["全部(" + data.data.asum.toFixed(2) + ")元", "待结算(" + data.data.bsum.toFixed(2) + ")元", "已结算(" + data.data.csum.toFixed(2) + ")元" ];
         self.setData({
           bars: bars
         })
    }, true);
	  this.getList();
  },
  getList: function () {
    var self = this;

    if (self.data.nexts[this.data.tab] == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = { type: this.data.tab, pageNo: parseInt(self.data.pages[this.data.tab]), pageSize: self.data.size };
    ux.request(self.data.action, params, function (data) {
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        var nexts = self.data.nexts;
        nexts[self.data.tab] = true;
        self.setData({
          nexts: nexts
        });
      } else {
        var nexts = self.data.nexts;
        nexts[self.data.tab] = false;
        self.setData({
          nexts: nexts
        });
      }
      if (self.data.pages[self.data.tab] == 0) {
        var lists = self.data.list;
        lists[self.data.tab] = datas;
        self.setData({
          list: lists
        });
      } else {
        var lists = self.data.list;
        lists[self.data.tab] = lists[self.data.tab].concat(datas);
        self.setData({
          list: lists
        });
      }
      var pages = self.data.pages;
      pages[self.data.tab] = pages[self.data.tab] + 1;
      self.setData({
        pages: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    }, true);
  },
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  loadMore: function () {
    console.log("ddddddddddddd");
    this.getList();
  },
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val+""!=""){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },
  onBar: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val + "" != "") {
      if (dataset.val != this.data.tab) {
        if (this.data.list[dataset.val].length > 0) {
          data[dataset.key] = dataset.val;
        } else {
          data[dataset.key] = dataset.val;
          this.getList();
        }
      } else {
        data[dataset.key] = dataset.val;
      }

    }
    this.setData(data);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})
